#include<stdio.h>
#include<at89c51ed2.h>
#include<mcs51reg.h>
#include<malloc.h>


extern void i2c_init(int block_select);
extern void i2c();
extern void i2c_read();
extern int i2c_write();
extern int i2c_sendData(int val);
extern int i2c_readData();
extern void i2c_stop();
extern int i2c_ack();
extern void i2c_noack();




void delay(int val){
	
	int i;
	i=0;
	while(i<val)
	{
		i++;
	}
}


///*acknowledge polling*/
void deviceBusy(){
int ack;

ack=1;
delay(100);
	while(ack==1){
	delay(100);
	i2c_init(0);
	ack=i2c_write();
	
	//ack=i2c_ack();
	//printf("%d",ack);
	}

}




int eebytew(int addr,int value){
	int blck_select;
	int ack;

	deviceBusy();
	
	if(addr>0&&addr<256){
		blck_select=0;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>255&&addr<512){
		blck_select=1;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>511&&addr<768){
		blck_select=2;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>767&&addr<1024){
		blck_select=3;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1023&&addr<1280){
		blck_select=4;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1279&&addr<1536){
		blck_select=5;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1535&&addr<1792){
		blck_select=6;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1791&&addr<2049){
		blck_select=7;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	
	/*send address*/
	
	i2c_write();
	ack=i2c_sendData(addr);
	ack=i2c_sendData(value);
	i2c_stop();
	return ack;
}
		
		
		
int * eebyteSeqr(int addr,int numBytes){
	int blck_select;
	int ack;	
	
	int count;
	xdata int * hexDump;
	
	hexDump=(xdata int *)malloc(500);
	
	deviceBusy();
	
	if(addr>0&&addr<256){
		blck_select=0;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>255&&addr<512){
		blck_select=1;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>511&&addr<768){
		blck_select=2;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>767&&addr<1024){
		blck_select=3;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1023&&addr<1280){
		blck_select=4;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1279&&addr<1536){
		blck_select=5;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1535&&addr<1792){
		blck_select=6;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1791&&addr<2049){
		blck_select=7;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	
	
	i2c_write();
	ack=i2c_sendData(addr);
	i2c_init(blck_select);
	i2c_read();
	
	
	count=0;
	while(count<numBytes){
		
	hexDump[count]=i2c_readData();
	
	count++;
	i2c_noack();

	}
	ack=i2c_ack();
	i2c_stop();
	return hexDump;
}



	
	

int eebyter(int addr){
	int blck_select;
	int ack;	
	int readV;
	
		deviceBusy();
	
	if(addr>0&&addr<256){
		blck_select=0;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>255&&addr<512){
		blck_select=1;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>511&&addr<768){
		blck_select=2;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>767&&addr<1024){
		blck_select=3;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1023&&addr<1280){
		blck_select=4;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1279&&addr<1536){
		blck_select=5;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1535&&addr<1792){
		blck_select=6;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1791&&addr<2049){
		blck_select=7;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	
	
	
	
	
	
	i2c_write();
	ack=i2c_sendData(addr);
	i2c_init(blck_select);
	i2c_read();
	i2c_readData();
	ack=i2c_ack();
//	if(ack==1){
//			printf("\n\rRead Success");
//		}
//		else{
//			printf("\n\rRead z");
//		}
		
	i2c_stop();
	return readV;



}
